{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "\"\"\"\n",
    "Configuration\n",
    "\"\"\"\n",
    "\n",
    "from transwarp.db import Dict\n",
    "\n",
    "import config_default\n",
    "\n",
    "\n",
    "def merge(defaults, override):\n",
    "    \"\"\"\n",
    "    合并override 和 default 配置文档，返回字典\n",
    "    \"\"\"\n",
    "    r = {}\n",
    "    for k, v in defaults.iteritems():\n",
    "        if k in override:\n",
    "            if isinstance(v, dict):\n",
    "                r[k] = merge(v, override[k])\n",
    "            else:\n",
    "                r[k] = override[k]\n",
    "        else:\n",
    "            r[k] = v\n",
    "    return r\n",
    "\n",
    "\n",
    "def toDict(d):\n",
    "    \"\"\"\n",
    "    将一个字典对象转换成 一个Dict对象\n",
    "    \"\"\"\n",
    "    D = Dict()\n",
    "    for k, v in d.iteritems():\n",
    "        D[k] = toDict(v) if isinstance(v, dict) else v\n",
    "    return D\n",
    "\n",
    "configs = config_default.configs\n",
    "\n",
    "\n",
    "\n",
    "try:\n",
    "    import config_override\n",
    "    configs = merge(configs, config_override.configs)\n",
    "except ImportError:\n",
    "    pass\n",
    "\n",
    "configs = toDict(configs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
